<script setup>
import pageContainer from '@/components/page-container.vue';
import { Plus } from '@element-plus/icons-vue'
import { ref } from 'vue';
import {useUserStore} from '@/stores/modules/user'
import {userAvatorUpdateService} from '@/api/userLogin'
// import { ElMessage } from 'element-plus';
const userStore = useUserStore()
const imgUrl =ref(userStore.user.user_pic)

const upLoadRef = ref()
const onUploadFile = (uploadFile) => {
  const reader = new FileReader()
  reader.readAsDataURL(uploadFile.raw)
  reader.onload = () => {
    // const base64Data = reader.result.split(',')[1];
    // imgUrl.value = base64Data
    imgUrl.value = reader.result
    // console.log(base64Data);
  }
}


const UpdateAvatar = async()=>{
    console.log(imgUrl.value);
    await userAvatorUpdateService(imgUrl.value)
    await userStore.getUser()
    ElMessage.success('更换成功')
}
</script>

<template>
    <page-container title="更改头像">
        <el-upload
        class="avatar-uploader"
        :show-file-list="false"
        :auto-upload="false"
        ref="upLoadRef"
        :on-change="onUploadFile"
        >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
        <br>
        <el-button @click="UpdateAvatar">↑ 上传头像</el-button>
    </page-container>   
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>